﻿#region

using System;
using System.Data.Entity;
using System.Linq;
using xConnected.Core.Common.Paging;
using xConnected.Core.Interfaces.Data;
using xConnected.Core.Interfaces.Paging;
using xConnected.Core.Model;
using xConnected.Core.Model.ViewModel.xConnected.Core.Model.ViewModel;

#endregion

namespace xConnected.Data
{
    public partial class CompanyProfileRepository : BaseRepository<CompanyProfile>, ICompanyProfileRepository
    {
        public override CompanyProfile GetById(int id)
        {
            return DbSet
                .Include(p => p.User)
                .Include(p => p.User.ExpertProfiles)
                .Include(p => p.User.CompanyProfiles)
                .Include(p => p.User.CompanyProfiles.Select(c => c.Positions))
                .Include(p => p.Categories)
                .Include(p => p.Technologies)
                .Include(p => p.Country)
                .Include(p => p.Industry)
                .Include(p => p.Positions).SingleOrDefault(p => p.Id == id);
        }

        public override IQueryable<CompanyProfile> GetAll()
        {
            return DbSet
                .Include(p => p.User)
                .Include(p => p.Country)
                .Include(p => p.Industry)
                .Include(p => p.Categories)
                .Include(p => p.Technologies);
        }

        //PAGING

        #region ICompanyProfileRepository Members

        public override void Delete(CompanyProfile entity)
        {
            DbSet.Remove(DbSet.Single(c => c.Id == entity.Id));
        }

        //ADVANCED SEARCH
        public IPage<CompanyProfile> AdvancedPagedSearchedAll(AdvancedSearchModelCompany companyModel)
        {
            var data = AdvancedSearchAll(companyModel);
            //if method is called from MyProfiles mode and if ShowAll is selected return all profiles
            //if ShowAll is not selected return only first 3 profiles
            companyModel.PageSize = companyModel.UserId != 0
                                       ? companyModel.ShowAll ? data.Count() : 3
                                       : companyModel.PageSize;
            var pagedData =
                data.OrderBy(k => k.Id).Skip(companyModel.PageSize*companyModel.PageIndex).Take(companyModel.PageSize).
                    AsEnumerable();
            return new Page<CompanyProfile>(pagedData, data.Count(), companyModel.PageSize, companyModel.PageIndex);
        }

        public IPage<CompanyProfile> AdvancedPagedSearchedSome(AdvancedSearchModelCompany companyModel)
        {
            var data = AdvancedSearchSome(companyModel);
            var pagedData =
                data.OrderBy(k => k.Id).Skip(companyModel.PageSize*companyModel.PageIndex).Take(companyModel.PageSize).
                    AsEnumerable();
            return new Page<CompanyProfile>(pagedData, data.Count(), companyModel.PageSize, companyModel.PageIndex);
        }

        #endregion

        private IQueryable<CompanyProfile> AdvancedSearchAll(AdvancedSearchModelCompany companyModel)
        {
            IQueryable<CompanyProfile> companies = companyModel.UserId == 0
                                                       ? DbSet
                                                       : DbSet.Where(c => c.User.Id == companyModel.UserId);

            if (companyModel.Keywords != null && companyModel.Keywords.Count != 0)
            {
                //if (companyModel.SimpleSearch)
                //{
                //    foreach (var keyword in companyModel.Keywords)
                //    {
                //        companies =
                //            companies.Where(
                //                c =>
                //                c.Description.ToLower().Contains(keyword.ToLower()) ||
                //                c.TagLine.ToLower().Contains(keyword.ToLower()) ||
                //                c.Positions.Any(p => p.Description.ToLower().Contains(keyword.ToLower()) ||
                //                                    p.Title.ToLower().Contains(keyword.ToLower())));
                //    }
                //    companies =
                //          companies.Where(
                //              c => companyModel.Keywords.Any(k => k.ToLower().Contains(c.Description.ToLower())) ||
                //                   companyModel.Keywords.Any(k => k.ToLower().Contains(c.TagLine.ToLower())) ||
                //                   companyModel.Keywords.Any(k => k.ToLower().Contains(
                //                       c.Positions.Any(p => p.Description.ToLower()) ||
                //                                           p.Title.ToLower());
                //}
                //else
                //{

                        companies =
                            companies.Where(
                                c => companyModel.Keywords.Any(k => k.ToLower().Contains(c.Description.ToLower())) ||
                                     companyModel.Keywords.Any(k => k.ToLower().Contains(c.TagLine.ToLower())));
                //}
            }
            if (!String.IsNullOrEmpty(companyModel.Title))
            {
                if (companyModel.Title.Contains(" "))
                {
                    string[] TitleSplit = companyModel.Title.Split(' ');
                    companies = companies.Where(c => TitleSplit.Any(t => t.ToLower().Contains(c.Title.ToLower())) || c.Title.ToLower().Contains(companyModel.Title.ToLower()));
                }
                else
                {
                    companies = companies.Where(c => c.Title.ToLower().Contains(companyModel.Title.ToLower()));
                }
            }
            if (!String.IsNullOrEmpty(companyModel.Location))
            {
                companies = companies.Where(p => p.Location.ToLower().Contains(companyModel.Location.ToLower()));
            }
            if (companyModel.CountryIds != null && companyModel.CountryIds.Count != 0)
            {
                companies = companyModel.CountryIds[0] != -1
                                ? companies.Where(c => companyModel.CountryIds.Contains(c.CountryId))
                                : companies;
            }
            if (companyModel.IndustryIds != null && companyModel.IndustryIds.Count != 0)
            {
                companies = companyModel.IndustryIds[0] != -1
                                ? companies.Where(c => companyModel.IndustryIds.Contains(c.Industry.Id))
                                : companies;
            }

            return companies.Include(p => p.User)
                            .Include(p => p.Country)
                            .Include(p => p.Industry)
                            .Include(p => p.Categories)
                            .Include(p => p.Technologies)
                            .AsNoTracking(); 
        }

        private IQueryable<CompanyProfile> AdvancedSearchSome(AdvancedSearchModelCompany companyModel)
        {
            IQueryable<CompanyProfile> companies = GetAll();
            var companiesList = companies.Where(c => c.Id == 0);
            IQueryable<CompanyProfile> companiesKeywords;
            IQueryable<CompanyProfile> companiesTitle;
            IQueryable<CompanyProfile> companiesAddress;
            IQueryable<CompanyProfile> companiesCountries;
            IQueryable<CompanyProfile> companiesIndustries;

            if (companyModel.Keywords != null)
            {
                //if (companyModel.SimpleSearch)
                //{
                //    foreach (var keyword in companyModel.Keywords)
                //    {
                //        companiesKeywords =
                //        companies.Where(
                //            c =>
                //            c.Title.ToLower().Contains(companyModel.Keywords.ToLower()) ||
                //            c.Description.ToLower().Contains(companyModel.Keywords.ToLower()) ||
                //            c.Location.ToLower().Contains(companyModel.Location.ToLower()) ||
                //            c.TagLine.ToLower().Contains(companyModel.TagLine.ToLower()) ||
                //            c.Positions.Any(p => p.Description.ToLower().Contains(companyModel.Keywords.ToLower()) ||
                //                                p.Title.ToLower().Contains(companyModel.Keywords.ToLower())) ||
                //            c.Country.Description.ToLower().Contains(companyModel.Keywords.ToLower()) ||
                //            c.Country.ShortName.ToLower().Contains(companyModel.Keywords.ToLower()) ||
                //            c.Industry.Description.ToLower().Contains(companyModel.Keywords.ToLower()));
                //    }
                    
                //}
                //else
                //{
                    companiesKeywords =
                        companies.Where(
                           c => companyModel.Keywords.Any(k => k.ToLower().Contains(c.Description.ToLower())) ||
                                     companyModel.Keywords.Any(k => k.ToLower().Contains(c.TagLine.ToLower())));
                //}
                companiesList = companiesList.Union(companiesKeywords);
            }

            if (companyModel.Title != null)
            {
                    if (companyModel.Title.Contains(" "))
                    {
                        string[] TitleSplit = companyModel.Title.Split(' ');
                        companiesTitle = companies.Where(c => TitleSplit.Any(t => t.ToLower().Contains(c.Title.ToLower())) || c.Title.ToLower().Contains(companyModel.Title.ToLower()));
                    }
                    else
                    {
                        companiesTitle = companies.Where(c => c.Title.ToLower().Contains(companyModel.Title.ToLower()));
                    }
                    companiesList = companiesList.Union(companiesTitle);
            }
            if (companyModel.Location != null)
            {
                companiesAddress =
                    companies.Where(c => c.Location.ToLower().Contains(companyModel.Location.ToLower()));
                companiesList = companiesList.Union(companiesAddress);
            }
            if (companyModel.CountryIds != null)
            {
                if (companyModel.CountryIds[0] != -1)
                {
                    companiesCountries =
                        companies.Where(c => companyModel.CountryIds.Contains(c.CountryId));
                    companiesList = companiesList.Union(companiesCountries);
                }
            }
            if (companyModel.IndustryIds != null)
            {
                if (companyModel.IndustryIds[0] != -1)
                {
                    companiesIndustries =
                        companies.Where(c => companyModel.IndustryIds.Contains(c.Industry.Id));
                    companiesList = companiesList.Union(companiesIndustries);
                }
            }

            return companiesList;
        }
    }
}